package com.maochang.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * 首页的页面跳转
 */
@Slf4j
@Controller
public class temlpate {

    //首页
    @GetMapping("/index")
    public String index() throws IOException {
        return "index";
    }
    //得治兰访问登录页面
    @GetMapping("/login")
    public String login(Model model) throws IOException {
        return "login";
    }
    @GetMapping("/kk")
    public String loginkk(Model model) throws IOException {
        return "login";
    }
    @GetMapping("/sy")
    public String sy(Model model) throws IOException {
        return "MyWorkPingTai/wdWordPingTai";
    }
    String YanZhengMa=null;

    //登录
    @ResponseBody
    @PostMapping("/login")
    public String loginFangFa(@RequestParam("name") String name,@RequestParam("pwd") String pwd,@RequestParam("yzm") String yzm,Model model,HttpSession httpSession) throws IOException {
        System.out.println(name+"--"+pwd+"--"+yzm);
        //4.创建 一个subject 实例 该实例的认证要使用上边创建的securityManager 进行
        Subject subject= SecurityUtils.getSubject();
        //声明令牌对象
        UsernamePasswordToken token=new UsernamePasswordToken(name,pwd);
        //System.out.println(YanZhengMa+"aa");
        if(YanZhengMa.equals(yzm)){
            try{
                //System.out.println("这是个登录方法");
                subject.login(token);
                //登录用户放到会话里  三种方法
                //1.直接采用httpSession
                //2.把 web 安全管理器的session  给他还原回去  成默认的
                //3.配置默认的session
                   //shirosession  可以将httpsession  和 shiro 进行同步  得到用户名
                   // subject.getSession().setAttribute("loginUser",subject.getPrincipal());
                   //httpSession
                   //   System.out.println(subject.getPrincipal()+"名字");
                      httpSession.setAttribute("loginUser",subject.getPrincipal());
                return "成功";
            }catch (Exception exception){
                log.error("用户登录失败",exception);
                return "失败";
            }
        }else {
            return "验证码错误-请重新输入验证码";
        }
    }

    //验证码
    /**
     * 获取验证码图片
     * @return
     */
    @RequestMapping("/getAuthCodeImg")
    public void    getAuthCodeImg(HttpSession session, HttpServletResponse response, Integer count,Model model){
        //定义图形验证码的长和宽  码值个数  干扰圈数
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(90, 40, 4, 10);
        BufferedImage codeImg = circleCaptcha.getImage();
        if (null != count && count > 0){
            //重新生成验证码
            circleCaptcha.createCode();
        }
        //String codeImg = lineCaptcha.getImageBase64();
        String authCode = circleCaptcha.getCode();
        YanZhengMa=authCode;
        session.setAttribute("aa","aa");
        if(session.getAttribute("validateCode")!=null){
            session.removeAttribute("validateCode");
            session.setAttribute("validateCode", authCode);
        }else{
            session.setAttribute("validateCode", "请输入验证码");
        }
        ServletOutputStream sos;
        try {
            sos = response.getOutputStream();
            ImageIO.write(codeImg, "jpeg", sos);
            sos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
